'''
从数据库查询数据进行断言
'''
import pymysql
from Tools.environment import Environment


class MysqlAssert:

    def __init__(self):
        # 82
        self.conn = pymysql.connect(**Environment().mysql_env())
        # 164
        # self.conn = pymysql.connect(**get_mysql_config('SIT_MYSQL_LG'))

    def get_count(self, table, column_dic):
        '''
        获取数据的条数
        :return:
        '''
        try:
            c = self.conn.cursor(pymysql.cursors.DictCursor)
            count = c.execute(sql)
        except Exception as E:
            raise ValueError('数据库操作异常')
        else:
            c.close()
            return count

    def delete_data_by_sql(self, delete_sql):
        '''
        根据sql删除数据库当中的数据
        :param delete_sql:
        :param sql:
        :return:
        '''
        try:
            c = self.conn.cursor()
            num = c.execute(delete_sql)
            self.conn.commit()
            c.close()
        except:
            print(delete_sql)
            raise ValueError('数据删除失败')
        else:
            return num

    def get_data(self, select_sql):
        '''
        获取数据库已存在的数据
        :param select_sql:
        :return:
        '''
        try:
            c = self.conn.cursor()
            c.execute(select_sql)
            data = c.fetchone()
        except Exception as E:
            raise ValueError('数据库操作异常')
        else:
            c.close()
            if data:
                return data[0]
            else:
                return None

    def get_datas(self, select_sql):
        '''
        获取数据库已存在的数据
        :param select_sql:
        :return:
        '''
        try:
            c = self.conn.cursor()
            c.execute(select_sql)
            data = c.fetchall()
        except Exception as E:
            raise ValueError('数据库操作异常')
        else:
            c.close()
            return data


if __name__ == '__main__':
    sql = 'DELETE FROM info_business WHERE NAME = "UI自动化商户"'
    sql1 = 'DELETE t FROM info_renovation t WHERE t.title LIKE "%UI%"'
    print(MysqlAssert().delete_data_by_sql(sql1))
